<?php


namespace app\api\service;


use app\common\model\ChartPersonal;

class ChartService extends ApiBaseService
{
    /**
     * @param array $param
     * @return bool
     * @throws \Exception
     */
    public function savePersonal(array $param): bool {
        $model = new ChartPersonal();
        $data = $model->where('user_id',$param['user_id'])->findOrEmpty();
        if (!$data->isEmpty()){
            $data->user_id = $param['user_id'];
        }
        $data->allowField(['user_id','name','birth','timezone','timezone_text'])
            ->save($param);
        if (empty($data->id)){
            throw new \Exception("保存个人人类图失败！");
        }
        return true;
    }

    /**
     * @param int $user_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPersonalByUserId(int $user_id): array {

        $model = new ChartPersonal();
        return $model->where('user_id',$user_id)->find()->toArray();
    }
}